#coding:utf-8
from appium import webdriver
from selenium.common.exceptions import NoSuchElementException
import os
import time
import logging
import logging.config
from selenium.webdriver.common.by import By

from Foo.capability_json import getDriver
from util.baseView import BaseView


class Login(BaseView):
    CON_LOG = '../docs/log.conf'
    logging.config.fileConfig(CON_LOG)
    def __init__(self,driver):
        BaseView.__init__(self,driver)
        self.num2 = (By.ID, 'com.sykean.biometrics:id/btn_key_2')
        self.num5 = (By.ID, 'com.sykean.biometrics:id/btn_key_5')
        self.num8 = (By.ID, 'com.sykean.biometrics:id/btn_key_8')
        self.num3 = (By.ID, 'com.sykean.biometrics:id/btn_key_3')
        self.num6 = (By.ID, 'com.sykean.biometrics:id/btn_key_6')
        self.num9 = (By.ID, 'com.sykean.biometrics:id/btn_key_9')
        self.home = (By.ID, 'com.sykean.biometrics:id/btn_home')

    def login(self):
        logging.info('显示等待5S')
        self.driver.implicitly_wait(5)
        self.checkLogin()

    def checkLogin(self):
        try:
            logging.info('寻找登录按钮')
            loginBtn = self.driver.find_element_by_id('com.sykean.biometrics:id/image_logo')
        except NoSuchElementException:
            print('登录按钮不存在')
        try:
            logging.info('启动登录')
            loginBtn.click()
            logging.info('输入258369')
            # for i in range(6):
            #     driver.find_element_by_id('com.sykean.biometrics:id/btn_key_6').click()
            #     time.sleep(1)
            self.click_element(*self.num2)
            self.click_element(*self.num5)
            self.click_element(*self.num8)
            self.click_element(*self.num3)
            self.click_element(*self.num6)
            self.click_element(*self.num9)

            '''
            临时测试，无需保存下面的代码
            '''
            self.click_element(*self.home)
        except:
            logging.info('-----未找到密码输入按键-----')
        # finally:
        #     self.click_element(*self.home)
        #     time.sleep(5)


if __name__ == '__main__':
    driver = getDriver()
    while True:
        Login(driver).login()
